package com.freen.fadmin.config.authenticationhandler.jwt.sysuser;

import com.freen.fadmin.entity.sys.SysuserLoginLog;
import com.freen.fadmin.repository.sys.SysuserLoginLogRepository;
import com.freen.fadmin.util.IpUtils;
import com.freen.fadmin.util.JwtSysuserTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

@Component("jwtSysuserAuthenticationSuccessHandler")
public class JwtSysuserAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Autowired
    private SysuserLoginLogRepository sysuserLoginLogRepository;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal != null && principal instanceof UserDetails){
            UserDetails user = (UserDetails) principal;
            //1、添加 Session
            request.getSession().setAttribute("userDetail",user);
            //2、写入日志 待定
            //3、写入数据库 SysuserLog
            SysuserLoginLog sysuserLoginLog = new SysuserLoginLog();
            sysuserLoginLog.setLoginip(IpUtils.getIpAddr(request));
            sysuserLoginLog.setLogintime(System.currentTimeMillis());
            sysuserLoginLog.setSysusername(user.getUsername());
            sysuserLoginLog.setStates(1);
            sysuserLoginLog.setWay(2);
            sysuserLoginLogRepository.save(sysuserLoginLog);

            String role = "";
            Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
            for (GrantedAuthority authority:authorities){
                role = authority.getAuthority();
            }
            String token = JwtSysuserTokenUtils.createToken(user.getUsername(),role,true);
            //System.out.println("role"+role);
            //response.setHeader("token",JwtSysuserTokenUtils.TOKEN_PREFIX+token);
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            //out.write("{\"code\":20000,\"message\":\"登录成功\"}");
            out.write("{\"rspCode\":\"000000\",\"rspMsg\":\"登录成功\",\"data\":{\"token\":\""+JwtSysuserTokenUtils.TOKEN_PREFIX+token+"\"}}");


            out.flush();
            out.close();
        }
    }
}
